﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace OpenXMLProcess
{
    public class ParagraphItem
    {
        private XElement _element;
        private ParagraphItem _parent;

        public XElement Element { get { return _element; } set { _element = value; } }
        public ParagraphItem Parent { get { return _parent; } set { _parent = value; } }
        public XElement Format
        {
            get
            {
                if (_element.Element(MyDocument.w + "pPr") == null)
                {
                    XElement e = new XElement(MyDocument.w + "pPr", "<w:sz w:val='13' />");
                    _element.AddFirst(e);
                    return _element.Element(MyDocument.w + "pPr");
                }
                return _element.Element(MyDocument.w + "pPr");
            }
        }
        public string Style
        {
            get
            {
                if (Format.Element(MyDocument.w + "pStyle") != null)
                    return Format.Element(MyDocument.w + "pStyle").Attribute(MyDocument.w + "val").Value;
                return "Normal";
            }
        }
        public List<ParagraphItem> Items { get; set; }
        public int FontSize
        {
            get
            {
                return int.Parse(Format.Element(MyDocument.w + "sz").Attribute(MyDocument.w + "val").Value);
            }
            set
            { Format.Element(MyDocument.w + "sz").Attribute(MyDocument.w + "val").Value = value.ToString(); }
        }
        public string Text
        {
            get
            {
                string temp = "";
                foreach (var r in GetRunsList())
                {
                    temp += r.Text;
                }
                return temp;
            }
        }
        public string TextFormat
        {
            get
            {
                string temp = "";
                foreach (var r in GetRunsList())
                {
                    temp += r.Text;
                }
                return temp+" ||| "+Style;
            }
        }

        public List<ParagraphItem> DocumentPath {
            get
            {
                var temp = new List<ParagraphItem>();
                ParagraphItem p=Parent;
                while(p!=null)
                {
                    temp.Add(p);
                    p = p.Parent;
                }
                return temp;
            }
        }

        public ParagraphItem(XElement element, ParagraphItem parent)
        {
            _element = element;
            _parent = parent;
            Items = new List<ParagraphItem>();
        }

        public bool IsParagraphItem()
        {
            return _element.Name.LocalName.Equals("p");
        }
        public List<RunItem> GetRunsList()
        {
            return _element.Descendants(MyDocument.w + "r").Select(r => new RunItem(r)).ToList();
        }
        public bool HasStyle()
        {
            return _element.Element(MyDocument.w + "pStyle") != null;
        }
        public bool HasRun()
        {
            return _element.Descendants(MyDocument.w + "r").Count() > 0;
        }
    }
}
